.PHONY: build test install clean

BINARY=onyx-snapshotter
VERSION=2.0.0
BUILD_DIR=build
CMD_DIR=cmd/onyx-snapshotter

LDFLAGS=-ldflags "-X main.Version=$(VERSION)"

build:
	@echo "Building $(BINARY) v$(VERSION)..."
	@mkdir -p $(BUILD_DIR)
	@go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY) ./$(CMD_DIR)

test:
	@echo "Running tests..."
	@go test -v -race -coverprofile=coverage.out ./...

test-integration:
	@echo "Running integration tests..."
	@go test -v -tags=integration ./tests/...

benchmark:
	@echo "Running benchmarks..."
	@go test -bench=. -benchmem ./...

install: build
	@echo "Installing $(BINARY)..."
	@sudo cp $(BUILD_DIR)/$(BINARY) /usr/local/bin/
	@sudo mkdir -p /etc/onyx /var/lib/onyx /run/onyx
	@sudo cp config/config.yaml /etc/onyx/
	@./scripts/install.sh

clean:
	@echo "Cleaning..."
	@rm -rf $(BUILD_DIR)
	@go clean

fmt:
	@echo "Formatting code..."
	@go fmt ./...

lint:
	@echo "Running linters..."
	@golangci-lint run

deps:
	@echo "Downloading dependencies..."
	@go mod download
	@go mod tidy

run: build
	@echo "Running $(BINARY)..."
	@sudo $(BUILD_DIR)/$(BINARY) \
		--config config/config.yaml \
		--address /run/onyx/onyx.sock \
		--log-level debug \
		--metrics-addr :9090

dev-setup:
	@echo "Setting up development environment..."
	@go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
	@go install github.com/stretchr/testify@latest

metrics:
	@echo "Fetching metrics..."
	@curl -s http://localhost:9090/metrics | grep onyx_

docker-build:
	@echo "Building Docker image..."
	@docker build -t onyx-snapshotter:$(VERSION) .

docker-run:
	@echo "Running in Docker..."
	@docker run --rm --privileged \
		-v /var/lib/onyx:/var/lib/onyx \
		-v /run/containerd:/run/containerd \
		onyx-snapshotter:$(VERSION)
